package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.ReceiveGiftSortInfo;
import com.juyu.miliao.api.domain.vo.ReceiveGiftInfoByOutUserVo;
import com.juyu.miliao.api.service.GiftService;
import com.juyu.miliao.common.constant.ReceiveGiftSortRedisKey;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author xuanqicheng
 * @create 2019-28-14:37
 */
@Component
public class ReceiveGiftSortRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private GiftService giftService;

    @Autowired
    private UserInfoCache userInfoCache;

    public List<ReceiveGiftSortInfo> getReceiveGiftSortByPage(int startNum, int endNum, long userId) {

        List<ReceiveGiftSortInfo>  list = new ArrayList<>();

        String key = ReceiveGiftSortRedisKey.ZSET_RECEIVE_GIFT_SORT_BY_INCOME_USER_ID_KEY.getValue(Long.toString(userId));

        Set<ZSetOperations.TypedTuple<String>> receiveGiftSorts = stringRedisTemplate.opsForZSet()
                .reverseRangeWithScores(key, startNum, endNum);

        if(receiveGiftSorts!=null&&receiveGiftSorts.size()>0){
            for(ZSetOperations.TypedTuple<String> typedTuple : receiveGiftSorts){

                long outUserId = Long.parseLong(typedTuple.getValue());

                long gold = typedTuple.getScore().longValue();
                UserCacheVo outUser = userInfoCache.getUserCacheInfo(outUserId);
                ReceiveGiftSortInfo receiveGiftSortInfo = new ReceiveGiftSortInfo()
                        .setOutUserId(outUserId)
                        .setIsHost(outUser.getIsHost())
                        .setIsVip(outUser.getIsVip())
                        .setVipLevel(outUser.getVipLevel())
                        .setStar(outUser.getStar())
                        .setGold(gold);

                list.add(receiveGiftSortInfo);
            }
        }

        return list;

    }

    public List<ReceiveGiftInfoByOutUserVo> receiveGiftInfoByOutUserIdAndIncomeUserId(long inUserId, long outUserId) {
        List<ReceiveGiftInfoByOutUserVo> list = new ArrayList<>();
        String key = ReceiveGiftSortRedisKey.ZSET_SEND_GIFT_BY_SEND_OUT_USER_AND_INCOME_USER_KEY
                .getValue(outUserId+"",inUserId+"");

        Set<ZSetOperations.TypedTuple<String>> set = stringRedisTemplate.opsForZSet()
                .reverseRangeWithScores(key,0,4);

        if(set!=null&&set.size()>0){
            for(ZSetOperations.TypedTuple<String> typedTuple : set){
                int giftId = Integer.parseInt(typedTuple.getValue());
                Gift gift =giftService.getGiftById(giftId);
                //TODO 2019-04-16 wbf 临时策略
                if(gift == null){
                    continue;
                }
                int number = typedTuple.getScore().intValue();
                ReceiveGiftInfoByOutUserVo receiveGiftInfoByOutUserVo = new ReceiveGiftInfoByOutUserVo();
                receiveGiftInfoByOutUserVo.setGiftId(giftId);
                receiveGiftInfoByOutUserVo.setNumber(number);
                receiveGiftInfoByOutUserVo.setGiftIcon(gift.getIcon());
                receiveGiftInfoByOutUserVo.setFee(number*gift.getPrice());
                list.add(receiveGiftInfoByOutUserVo);
            }
        }

        Collections.sort(list, new Comparator<ReceiveGiftInfoByOutUserVo>() {
            @Override
            public int compare(ReceiveGiftInfoByOutUserVo u1, ReceiveGiftInfoByOutUserVo u2) {
                int diff = u2.getFee() - u1.getFee();
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                }
                return 0;
            }
        });

        return list;

    }
}
